package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Employee;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;
import java.util.List;
import java.util.Set;


@RestController
@RequestMapping("/admin/dish")
@Slf4j
@Api(value = "菜品管理")
public class DishController {
    @Autowired
    private DishService dishService;
    @Autowired
    private RedisTemplate redisTemplate;
    /**
     * 新增菜品
     * @param dto
     * @return
     */
    @PostMapping
    @Transactional
    public Result insertDish(@RequestBody DishDTO dto){
        log.info("新增菜品"+dto);
        //清理缓存数据
        String key = "dish_" + dto.getCategoryId();
        cleanCache(key);
        return dishService.insertDish(dto);
    }
    /**
     * 根据id查询数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询数据")
    public Result getByiId(@PathVariable Long id){
        log.info("根据id查询数据"+id);
        return Result.success(dishService.getByiId(id)) ;
    }

    /**
     * 分页查询
     * @param dto
     * @return
     */
    @GetMapping("/page")
    public Result<PageResult> page(DishPageQueryDTO dto){
        log.info("分页查询"+dto);
        return dishService.page(dto);
    }

    /**
     * 修改菜品
     * @param dto
     * @return
     */
    @PutMapping
    @Transactional
    public Result updateDish(@RequestBody DishDTO dto){
        log.info("修改菜品"+dto);
        //将所有的菜品缓存数据清理掉，所有以dish_开头的key
        cleanCache("dish_*");
        return dishService.updateDish(dto);
    }
    /**
     * 菜品批量删除
     *
     * @param ids
     * @return
     */
    @DeleteMapping
    @ApiOperation("菜品批量删除")
    @Transactional
    public Result delete(@RequestParam List<Long> ids) {
        log.info("菜品批量删除：{}", ids);
        //将所有的菜品缓存数据清理掉，所有以dish_开头的key
        cleanCache("dish_*");
        return Result.success(dishService.delete(ids));
    }

    /**
     * 根据分类id查询菜品
     * @param categoryId
     * @return
     */
    @GetMapping("/list")
    public Result list(Long categoryId){
        log.info("根据分类id查询菜品"+categoryId);
        List<Dish> list = dishService.list(categoryId);
        return Result.success(list);
    }

    /**
     * 启售状态
     * @param status id
     * @return
     */
    @PostMapping("/status/{status}")
    public Result addStatus(@PathVariable Integer status,Long id){
        log.info("启售状态"+id);
        //将所有的菜品缓存数据清理掉，所有以dish_开头的key
        cleanCache("dish_*");
        return dishService.addStatus(status, id);
    }
/**
 * user
 */
    /**
     * 清理缓存数据
     * @param pattern
     */
    private void cleanCache(String pattern){
        Set keys = redisTemplate.keys(pattern);
        redisTemplate.delete(keys);
    }
}
